﻿using System;
using System.Collections.Generic;
using System.IO;
using Windows.UI.Xaml.Media.Imaging;
using System.Linq;
using System.Windows.Input;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Devices.Input;
using Windows.UI.Core;
using System.Threading;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace RoboRun
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainMenu : Page
    {

        public static float width; //Device width
        public static float height; //Device height
        public static float offset;
        bool offsetstate; //just fo reference yo

        public MainMenu()
        {
            this.InitializeComponent();
            obtainScreenDimensions();
            determineAspectRatio();
            loadLogo();
            loadButtons();
        }

        private void obtainScreenDimensions()
        {
            width = ScreenDimensions.obtainScreenWidth();
            height = ScreenDimensions.obtainScreenHeight();
        }

        private void determineAspectRatio()
        {
            if (width / height == 1.6) //implies 16:10 aspect ratio
            {
                offset = 40 * height / 800;
                offsetstate = false;
            }
            else
            {
                offset = 0;
                offsetstate = true;
            }
        }

        private void loadLogo()
        {
            logo.Source = new BitmapImage(new Uri("ms-appx:///Assets/MainMenu/mainmenulogo.png", UriKind.Absolute
                    ));
            logo.Height = 254 * height / 1080;
            logo.Width = 1181 * width / 1920;
            logo.Margin = new Thickness(364*width/1920,offset + 44*height/1080,0,0);
        }

        private async void loadButtons()
        {
            loadPlayButton();
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(200));
            loadTutorialsButton();
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(200));
            loadHelpButton();
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(200));
            playbutton.IsEnabled = true;
            tutorialsbutton.IsEnabled = true;
            helpbutton.IsEnabled = true;
        }

        private async void loadTutorialsButton()
        {
            tutorialsbuttonimage.Source = new BitmapImage(new Uri("ms-appx:///Assets/MainMenu/tutorialsbutton.png", UriKind.Absolute
                    ));
            tutorialsbuttonimage.Height = 174 * height / 1080;
            tutorialsbuttonimage.Width = 1140 * width / 1920;
            tutorialsbutton.Height = 174 * height / 1080;
            tutorialsbutton.Width = 1140 * width / 1920;
            for (float i = width; i >= 381 * width / 1920; i -= 25)
            {
                tutorialsbutton.Margin = new Thickness(i, offset + 265 * height / 1080, 0, 0);
                tutorialsbuttonimage.Margin = new Thickness(i, offset+ 265*height/1080, 0, 0);
                await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
            }
        }

        private async void loadPlayButton()
        {
            playbuttonimage.Source = new BitmapImage(new Uri("ms-appx:///Assets/MainMenu/playbutton.png", UriKind.Absolute
                    ));
            playbuttonimage.Height = 174 * height / 1080;
            playbuttonimage.Width = 1140 * width / 1920;
            playbutton.Height = 174 * height / 1080;
            playbutton.Width = 1140 * width / 1920;
            for (float i = width; i >= 381 * width / 1920; i -= 25)
            {
                playbutton.Margin = new Thickness(i, offset + 372 * height / 1080, 0, 0);
                playbuttonimage.Margin = new Thickness(i, offset + 372 * height / 1080, 0, 0);
                await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
            }
           
        }

        private void disableAllButtons()
        {
            playbutton.IsEnabled = false;
            tutorialsbutton.IsEnabled = false;
            helpbutton.IsEnabled = false;
        }

        private async void loadHelpButton()
        {
            helpbuttonimage.Source = new BitmapImage(new Uri("ms-appx:///Assets/MainMenu/helpbutton.png", UriKind.Absolute
                    ));
            helpbuttonimage.Height = 174 * height / 1080;
            helpbuttonimage.Width = 1140 * width / 1920;
            helpbutton.Height = 174 * height / 1080;
            helpbutton.Width = 1140 * width / 1920;
            for (float i = width; i >= 381 * width / 1920; i -= 25)
            {
                helpbutton.Margin = new Thickness(i, 0, 0, offset + 100 * height / 1080);
                helpbuttonimage.Margin = new Thickness(i, 0, 0, (100 * height / 1080) - (offset/3) + offset/3);
                await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
            }
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private async void removeButtons()
        {
            removeHelpButton();
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(200));
            removeTutorialsButton();
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(200));
            removePlayButton();
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(200));

        }

        private async System.Threading.Tasks.Task removePlayButton()
        {
            for (float i = 381 * width / 1920; i <= width + 100; i += 25)
            {
                playbutton.Margin = new Thickness(i, offset + 372 * height / 1080, 0, 0);
                playbuttonimage.Margin = new Thickness(i, offset + 372 * height / 1080, 0, 0);
                await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
            }
        }

        private async System.Threading.Tasks.Task removeTutorialsButton()
        {
            for (float i = 381 * width / 1920; i <= width + 100; i += 25)
            {
                tutorialsbutton.Margin = new Thickness(i, offset + 265 * height / 1080, 0, 0);
                tutorialsbuttonimage.Margin = new Thickness(i, offset + 265 * height / 1080, 0, 0);
                await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
            }
        }

        private async System.Threading.Tasks.Task removeHelpButton()
        {
            for (float i = 381 * width / 1920; i <= width + 100; i += 25)
            {
                helpbutton.Margin = new Thickness(i, 0, 0, offset + 50 * height / 1080);
                helpbuttonimage.Margin = new Thickness(i, 0, 0, (100 * height / 1080) - (offset / 3) + offset / 3);
                await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
            }
        }

        private void playbutton_Click_1(object sender, RoutedEventArgs e) //currently goes to gameplay screen. need a level select screen
        {
            disableAllButtons();
            goToLevelSelect();
        }

        private async void goToLevelSelect()
        {
            ButtonFlash.buttonFlash(playbuttonimage, new Uri("ms-appx:///Assets/MainMenu/playbuttoninverted.png", UriKind.Absolute
                  ), new Uri("ms-appx:///Assets/MainMenu/playbutton.png", UriKind.Absolute
                  ));
            removeButtons();
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1500));
            this.Frame.Navigate(typeof(LevelScreen));
        }

        private async void goToTutorials()
        {
            ButtonFlash.buttonFlash(tutorialsbuttonimage, new Uri("ms-appx:///Assets/MainMenu/tutorialsbuttoninverted.png", UriKind.Absolute
                  ), new Uri("ms-appx:///Assets/MainMenu/tutorialsbutton.png", UriKind.Absolute
                  ));
            removeButtons();
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1500));
            this.Frame.Navigate(typeof(TutorialSelectionPage));
        }

        private void tutorialsbutton_Click_1(object sender, RoutedEventArgs e)
        {
            disableAllButtons();
            goToTutorials();
            
        }

        private async void helpbutton_Click_1(object sender, RoutedEventArgs e)
        {
            disableAllButtons();
            ButtonFlash.buttonFlash(helpbuttonimage, new Uri("ms-appx:///Assets/MainMenu/helpbuttoninverted.png", UriKind.Absolute
                  ), new Uri("ms-appx:///Assets/MainMenu/helpbutton.png", UriKind.Absolute
                  ));
            removeButtons();
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1500));
            this.Frame.Navigate(typeof(HelpandAboutPage));
        }
    }
}
